package sns.action;

import java.util.Date;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import sns.bean.Comment;
import sns.bean.Mblog;
import sns.dao.CommentDao;
import sns.dao.MblogDao;
import sns.dao.UserDao;
import sns.service.CommentService;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class AddCommentAction extends ActionSupport {
	// injection
	MblogDao mblogDao;
	UserDao userDao;
	CommentDao commentDao;
	CommentService commentService;

	// pass in
	/**
	 * 是否要转发
	 */
	private String flagOriginal;

	private Boolean flag = false;
	private Long commentIdtargetmblog;
	private Long commentIdpublisher;
	private String commentContent;

	// return THIS SHOULD BE AJAX
	private List<Comment> commentList;
	private Long mblogId;
	private Long visitId;

	// parameter
	private Comment comment;
	private Mblog mblog;

	@Transactional
	public String execute() {
		if (visitId == null) {
			System.out.println("No visit id.");
			return ActionSupport.INPUT;
		}
		
		if (flagOriginal != null) {
			flag = true;
		}
		if (commentIdpublisher == null) {
			System.out.println("There is no publisher");
			return ActionSupport.INPUT;
		} else if (commentIdtargetmblog == null) {
			System.out.println("There is no target blog");
			return ActionSupport.INPUT;
		} else if (commentContent == null) {
			System.out.println("There is no content");
			return ActionSupport.INPUT;
		}
		comment = new Comment();
		comment.setTargetMblog(mblogDao.getMblogById(commentIdtargetmblog));
		comment.setPublisher(userDao.findUserById(commentIdpublisher));
		comment.setCommentTxt(commentContent);
		comment.setCommentTime(new Date());
		commentDao.save(comment);

		// 需要转发则跳转到publish action
		if (flag) {
			// set value
			mblog = new Mblog();

			// 如果目标微博也是转发微博，那么原始微博应该是
			if (mblogDao.getMblogById(commentIdtargetmblog).getOriginalMblog() != null) {
				mblog.setOriginalMblog(mblogDao.getMblogById(
						commentIdtargetmblog).getOriginalMblog());
			} else {
				mblog.setOriginalMblog(mblogDao
						.getMblogById(commentIdtargetmblog));
			}

			if (commentContent.equals("")) {
				commentContent = "转发微博" + commentContent;
			}

			if (commentContent.length() > 140) {
				commentContent = commentContent.substring(0, 140);
			}

			mblog.setPublisher(userDao.findUserById(commentIdpublisher));
			mblog.setMblogTxt(commentContent);
			mblog.setMblogTime(new Date());
			mblogDao.save(mblog);
			mblogId = commentIdtargetmblog;
			commentList = commentService.getCommentOfMblog(mblogDao
					.getMblogById(mblogId));

		}

		return Action.SUCCESS;

	}

	public List<Comment> getCommentList() {
		return commentList;
	}

	public Long getMblogId() {
		return mblogId;
	}

	public void setCommentService(CommentService commentService) {
		this.commentService = commentService;
	}

	public void setCommentContent(String content) {
		this.commentContent = content;
	}

	public void setMblogDao(MblogDao mblogDao) {
		this.mblogDao = mblogDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public void setCommentDao(CommentDao commentDao) {
		this.commentDao = commentDao;
	}

	public Boolean getFlag() {
		return flag;
	}

	public void setFlag(Boolean flag) {
		this.flag = flag;
	}

	public Long getCommentIdtargetmblog() {
		return commentIdtargetmblog;
	}

	public void setCommentIdtargetmblog(Long commentIdtargetmblog) {
		this.commentIdtargetmblog = commentIdtargetmblog;
	}

	public Long getCommentIdpublisher() {
		return commentIdpublisher;
	}

	public void setCommentIdpublisher(Long commentIdpublisher) {
		this.commentIdpublisher = commentIdpublisher;
	}

	public CommentService getCommentService() {
		return commentService;
	}
	

	public String getFlagOriginal() {
		return flagOriginal;
	}

	public void setFlagOriginal(String flagOriginal) {
		this.flagOriginal = flagOriginal;
	}
	

	public Long getVisitId() {
		return visitId;
	}

	public void setVisitId(Long visitId) {
		this.visitId = visitId;
	}

}
